package cn.edu.sjtu.se.dslab.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.component.Component;
import cn.edu.sjtu.se.dslab.component.data.DataComponent;
import cn.edu.sjtu.se.dslab.dao.IBaseDao;
import cn.edu.sjtu.se.dslab.dao.IComponentDao;
import cn.edu.sjtu.se.dslab.dao.IDataComponentDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.MD5;

@Repository("dataComponentDao")
public class DataComponentDaoImpl implements IDataComponentDao {

	@Resource(name = "baseDao")
	private IBaseDao<DataComponent> baseDao;

	@Resource(name = "componentDao")
	private IComponentDao componentDao;

	private static final String DELETED = "deleted";
	
	@Override
	public void save(DataComponent dataComponent) {
		// TODO Auto-generated method stub
		String temp = dataComponent.getDescription()
				+ "data"+ System.currentTimeMillis();
		MD5 md5 = new MD5();
		String uri = md5.getMD5ofStr(temp).substring(0, 8);
		while (!componentDao.checkUri(Const.URI_DATA_COMPONENT + ":" + uri)) {
			temp = dataComponent.getDescription()
					+ "data"+ System.currentTimeMillis();
			uri = md5.getMD5ofStr(temp).substring(0, 8);
		}
		dataComponent.setUri(Const.URI_DATA_COMPONENT + ":" + uri);
		baseDao.save(dataComponent);
	}

	@Override
	public void update(DataComponent dataComponent) {
		// TODO Auto-generated method stub
		baseDao.update(dataComponent);
	}

	@Override
	public DataComponent queryById(long id) {
		// TODO Auto-generated method stub
		return baseDao.queryById(DataComponent.class, id);
	}

	@Override
	public List<DataComponent> queryAll() {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataComponent where ");
		hql.append(DELETED + "=0");
		return baseDao.query(hql.toString());
	}

	@Override
	public PageBean<DataComponent> queryByPageNum(int pageNo)
			throws DBException {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		// 拼接sql语句
		hql.append("from DataComponent where ");
		hql.append(DELETED + "=0");
		PageBean<DataComponent> bean = baseDao.queryByspecialHqlForPage(pageNo,
				Const.PAGESIZE, hql.toString());
		return bean;
	}

	@Override
	public DataComponent queryByUri(String uri) {
		// TODO Auto-generated method stub
		Component component = componentDao.queryByUri(uri);
		if (component != null) {
			// DataComponent dataComponent =
			// baseDao.queryById(DataComponent.class,component.getId());
			// if (dataComponent.getChildList() != null) {
			// Set<DataComponent> components = new HashSet<DataComponent>();
			// for (DataComponent data:dataComponent.getChildList()) {
			// DataComponent temp = queryById(data.getId());
			// components.add(temp);
			// }
			// dataComponent.setChildList(components);
			// }
			// if (dataComponent.getDepends() != null) {
			// Set<DataComponent> components = new HashSet<DataComponent>();
			// for (DataComponent data:dataComponent.getDepends()) {
			// DataComponent temp = queryById(data.getId());
			// components.add(temp);
			// }
			// dataComponent.setDepends(components);
			// }
			return baseDao.queryById(DataComponent.class, component.getId());
		}
		return null;
	}

}
